package gui.custom;

import gui.BetterFlowLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.MediaTracker;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import backend.rss.items.ThemeItem;

public class RatingPanel extends JPanel
{
	private static String EMPTY_STAR = "resources/black star.png";
	private static String FILLED_STAR = "resources/pink star.png";
	// not used atm.. will be used if we handle logins
	private static final String USER_RATED_STAR = "resources/cyan star.png";

	private static ImageIcon FILLED_ICON = new ImageIcon(FILLED_STAR);
	private static ImageIcon EMPTY_ICON = new ImageIcon(EMPTY_STAR);	
	
	private final ThemeItem item;
	private List<JButton> starMap;

	static
	{
		if (FILLED_ICON.getImageLoadStatus() != MediaTracker.COMPLETE)
		{
			FILLED_ICON = new ImageIcon("../" + FILLED_STAR);
		}
		
		if (EMPTY_ICON.getImageLoadStatus() != MediaTracker.COMPLETE)
		{
			EMPTY_ICON = new ImageIcon("../" + EMPTY_STAR);
		}
	}
	
	public RatingPanel(final ThemeItem item)
	{
		super(new BetterFlowLayout(FlowLayout.LEFT, 2, 0));
		this.item = item;
		
		starMap = new ArrayList<JButton>(5);
		
		initStars();
		
		setSize(65, 10);
	}
	
	private void initStars()
	{
		int rating = item.getRating();
		Dimension buttonSize = new Dimension(10,10);
		for (int x = 0; x < rating; x++)
		{
			JButton button = new AAButton(FILLED_ICON);
			button.setSize(buttonSize);
			button.setPreferredSize(buttonSize);
			starMap.add(button);
		}
		
		while (starMap.size() < 5)
		{
			JButton button = new AAButton(EMPTY_ICON);
			button.setSize(buttonSize);
			button.setPreferredSize(buttonSize);
			starMap.add(button);
		}
		
		for (JButton button : starMap)
		{
			add(button);
		}
		
		repaint();
		validate();
	}
	
	
}
